package com.rem.chat.service.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.util.StringUtils;

@AllArgsConstructor
@Getter
public enum UserContactTypeEnum {
    USER(0, "U", "用户"),
    GROUP(1, "G", "群聊");

    private final Integer type;
    private final String prefix;
    private final String desc;

    public static UserContactTypeEnum getByType(Integer type) {
        try {
            for (UserContactTypeEnum item: UserContactTypeEnum.values()) {
                if (type.equals(item.getType())) {
                    return item;
                }
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }

    public static UserContactTypeEnum getByPrefix(String prefix) {
        try {
            if (!StringUtils.hasLength(prefix.trim())) {
                return null;
            }
            prefix = prefix.substring(0, 1);
            for (UserContactTypeEnum item: UserContactTypeEnum.values()) {
                if (prefix.equalsIgnoreCase(item.getPrefix())) {
                    return item;
                }
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }
}
